﻿#region

using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class TrackInputCtrl
    {
        public TrackInputCtrl(INotifyPropertyChanged context)
        {
            InitializeComponent();
            DataContext = context;
            var propertyChanged = DataContext as INotifyPropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged.PropertyChanged += OnPropertyChanged;
            }
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Artists":
                    Dispatcher.BeginInvoke(delegate { ArtistCtrl.PopulateComplete(); });
                    break;
                case "ArtistTracks":
                    Dispatcher.BeginInvoke(delegate { TrackCtrl.PopulateComplete(); });
                    break;
            }
        }

        #region Private fields

        private void ArtistKeyUp(object sender, KeyEventArgs e)
        {
            var txt = sender as AutoCompleteBox;
            if (txt != null)
            {
                switch (e.Key)
                {
                    default:
                        if (e.Key >= Key.A && e.Key <= Key.Z)
                        {
                            var dc = DataContext as TrackInputViewModel;
                            if (dc != null)
                            {
                                dc.AutoComplete.GetArtists(txt.Text);
                            }
                        }
                        break;
                }
            }
        }

        private void TrackKeyUp(object sender, KeyEventArgs e)
        {
            var txt = sender as AutoCompleteBox;
            if (txt != null)
            {
                switch (e.Key)
                {
                    default:
                        if (e.Key >= Key.A && e.Key <= Key.Z)
                        {
                            var dc = DataContext as TrackInputViewModel;
                            if (dc != null)
                            {
                                dc.AutoComplete.GetArtistTracks(ArtistCtrl.Text, txt.Text);
                            }
                        }
                        break;
                }
            }
        }

        #endregion
    }
}